package com.fellan.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.view.Window;
import com.fellan.R;

/**
 * Created by IntelliJ IDEA.
 * User: Edy
 * Date: 26.05.2012
 * Time: 14:57
 * To change this template use File | Settings | File Templates.
 */
public class ActivityUtils {
    /**
     * This is the dialog that is displayed on loading screens
     */
    private static Dialog thisDialog;

    /**
     * Method that navigates to another activity
     *
     * @param activity      : current activity
     * @param activityClass : class of the destination activity
     * @param finish        : if true current activity will be finished
     */
    public static void navigate(Activity activity, Class activityClass, boolean finish) {
        Intent intent = new Intent();
        intent.setClass(activity, activityClass);

        if (finish) {
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            activity.startActivity(intent);
            activity.setResult(Activity.RESULT_OK);
            activity.finish();
        } else {
            // Launching a child activity. We are interested in it's result so
            // we can end this activity also if needed when the child activity
            // ends
            activity.startActivityForResult(intent, Activity.RESULT_FIRST_USER);

        }
    }

    /**
     * Will show the progress dialog on a given activity
     *
     */
    public static void showProgressDialog(Activity activity) {

        if ((thisDialog == null) || (!thisDialog.isShowing())) {
            //Block the screen orientation
            activity.setRequestedOrientation(activity.getResources().getConfiguration().orientation);

            //Create a new dialog
            thisDialog = new Dialog(activity);

            //Redirect focus to this dialog
            thisDialog.getWindow().getCurrentFocus();
            thisDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            thisDialog.setContentView(R.layout.progress_dialog);

            //This is not cancelable at back pressed
            thisDialog.setCancelable(false);

            //Set the owner for this activity
            thisDialog.setOwnerActivity(activity);

            thisDialog.show();
        } else {
            thisDialog.setOwnerActivity(activity);

        }
    }

    /**
     * Will hide the progress dialog on a given activity
     *
     */
    public static void hideProgressDialog(Activity activity) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
        if (thisDialog != null && thisDialog.isShowing()
                && (activity == thisDialog.getOwnerActivity())) {
            try {
                thisDialog.dismiss();
            } catch (IllegalArgumentException e) {
            }
        }
    }
}
